var latlng = new google.maps.LatLng(31.2292969,121.459026);
var myOptions = {
	zoom: 14,
	center: latlng,
	streetViewControl: false,
	panControl: false,
	zoomControl: false,
	mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map;
var markersArray = [];
var overlay = null;

//附件场所查找条件
var currentCenter = "31.2292969,121.459026";
var selectedPlaceType = "";

$(document).ready(function(){
	$("#map_canvas").width($(document).width() - 350);
	$(window).resize(function(){
		$("#map_canvas").width($(document).width() - 350);
	});
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	google.maps.event.addListener(map, 'dragend', function() {
		if (overlay != null) {
			overlay.draw();
		}
		
		currentCenter = map.getCenter().lat() + "," + map.getCenter().lng();
		freshPlaces();
	});
	
	freshPlaces();
});

//添加标志
function addMarker(positen, title, placeId, map) {
	var marker = new google.maps.Marker({
	    position: positen,
	    title:title
	});
	
	//为标志添加点击事件
	google.maps.event.addListener(marker, 'click', function() {
		showPlaceDetail(placeId);
	});
	
	marker.setMap(map);
	markersArray.push(marker);
}

function clearOverlays() {
  if (markersArray) {
	for (i in markersArray) {
	  markersArray[i].setMap(null);
	}
	markersArray.length = 0;
  }
  
  hidePlaceDetail();
}

function showPlaceDetail(placeId) {
	var requestUrl = contextPath + "/get_place_detail";
	var params = {place_id:placeId};
	
	hideCommentDlg();
	
	if (overlay == null) {
		overlay = new infoWindow(map);
		$(overlay.div_).css("padding", "8px");
		$(overlay.div_).css("background", "url('../image/bg_infowindow.png') repeat-y");
	} 
	
	$.post(requestUrl, params, function(data){
		var place = data.resultData;
		
		latlng = new google.maps.LatLng(place.gpsLocation.split(",")[0], place.gpsLocation.split(",")[1]);
		map.setCenter(latlng);
		if (overlay != null) {
			overlay.bounds_ = map.getBounds();
		    overlay.draw();
		}

		var infoHtml = "";
		infoHtml +=
		infoHtml += "<div style='font-size: 18px; font-weight: bold; padding-top: 7px;'>" + place.placeName + "</div>";
		infoHtml += "<img src='../image/line_gray_white.png' width='96%' height='2px'/>";
		infoHtml += '<div>';
		infoHtml += '	<div style="width: 280px; float: left;">';
		infoHtml += '		<div style="font-size: 18px; font-weight: bold; padding-top: 10px;">服务指数:<img src="../image/icon_level' + place.generalSatisfaction + '.png" align="top"/></div>';
		infoHtml += '		<div style="padding: 10px 0px">好评 ' + place.goodNum + '% <img src="../image/icon_red_flower.png" align="top"/></div>';
		infoHtml += '		<div style="padding: 10px 0px">中评 ' + place.normalNum + '% <img src="../image/icon_orange_flower.png" align="top"/></div>';
		infoHtml += '		<div style="padding: 10px 0px">差评 ' + place.badNum + '% <img src="../image/icon_gray_flower.png" align="top"/></div>';
		infoHtml += '	</div>';
		infoHtml += '	<div style="float: left;">';
		infoHtml += '		<img src="../image/icon_merchant_big.png"/>';
		infoHtml += '	</div>';
		infoHtml += '	<div style="clear: both;"></div>';
		infoHtml += '</div>';
		infoHtml += '<div style="padding: 10px 0px">地址：' + place.address + '</div>';
		infoHtml += '<div style="padding: 10px 0px">电话' + place.officePhone + '</div>';
		infoHtml += '<div style="padding: 10px 0px">商家简介</div>';
		infoHtml += '<textarea style="width: 379px; height: 113px; border: none; background: url(\'../image/bg_info_textarea.png\') no-repeat;" disabled="disabled">' + place.merchantIntroduction + '</textarea><br/>';
		infoHtml += '<div><img id="btn_comment" class="pointer fade_effect" src="../image/btn_comment.png" onclick="showCommentDlg(' + place.id + ')"/></div>';
		infoHtml += '<div style="padding-top: 25px;">';
		if (place.placePhoto && 2 < place.placePhoto.length) {
			infoHtml += '	<div style="width: 30px; float: left; padding-top:4px;"><img class="pointer fade_effect" src="../image/btn_left.png" onclick="slideLeft();"/></div>';
		} else {
			infoHtml += '	<div style="width: 30px; float: left; padding-top:4px;"><img class="pointer fade_effect" src="../image/btn_left.png"/></div>';
		}
		infoHtml += '	<div id="div_image_slider" style="width: 320px; height: 123px; float: left; overflow: hidden; background: url(\'../image/bg_image_slider.png\') no-repeat;">';
		for(var i = 0; place.placePhoto && i < place.placePhoto.length; i++) {
			infoHtml += '		<image width="95px" height="105px" style="padding-left: 9px; margin-top: 9px;" src="' + WSContextPath + '/' + place.placePhoto[i] + '"/>';
		}
		infoHtml += '	</div>';
		if (place.placePhoto && 2 < place.placePhoto.length) {
			infoHtml += '	<div style="width: 30px; float: left; padding-top:4px;"><img class="pointer fade_effect" src="../image/btn_right.png" onclick="slideRight();"/></div>';
		} else {
			infoHtml += '	<div style="width: 30px; float: left; padding-top:4px;"><img class="pointer fade_effect" src="../image/btn_right.png"/></div>';
		}
		infoHtml += '	<div style="clear:both;"></div>';
		infoHtml += '</div>';
		infoHtml += '<div style="padding: 10px 0px; font-size: 15pt;">评价：</div>';
		infoHtml += '<div id="div_place_comments"></div>';

		$(overlay.div_).html(infoHtml);
		overlay.draw();
		addFadeEffect();
		
		showPlaceComments(placeId);
	}, "json");
}

function hidePlaceDetail() {
	hideCommentDlg();
	if (overlay != null) {
		overlay.setMap(null);
		overlay = null;
	}
}

function slideLeft() {
	$("#div_image_slider").find("img:last").after($("#div_image_slider").find("img:first"));
}

function slideRight() {
	$("#div_image_slider").find("img:first").before($("#div_image_slider").find("img:last"));
}

function showCommentDlg(placeId) {
	$("#dlg_comment").css("left", $("#btn_comment").offset().left - 110);
	$("#dlg_comment").css("top", $("#btn_comment").offset().top + 30);
	$("#dlg_comment").css("display", "");
}

function hideCommentDlg() {
	$("#dlg_comment").css("display", "none");
}

function changeArea(areaName, areGps){
	$("#slt_display_text").html(areaName);
	$("#slt_district_list").css("display", "none");
	
	//选择商圈
	if (areGps != "") {
		var gps = areGps.split(",");
		latlng = new google.maps.LatLng(gps[0], gps[1]);
		map.setCenter(latlng);
		
		currentCenter = areGps;
	} else {
		currentCenter = map.getCenter().lat() + "," + map.getCenter().lng();
	}
	
	//刷新场所列表
	freshPlaces();
}

function changePlaceType(typeName, typeId){
	$("#slt_place_type_text").html(typeName);
	$("#slt_place_type_list").css("display", "none");
	
	//选择场所种类
	selectedPlaceType = typeId;
	
	//刷新场所列表
	freshPlaces();
}

/*
 * 刷新场所列表
 */
function freshPlaces() {
	requestUrl = "../search_places";
	var params = {
		location:currentCenter,
		place_type:selectedPlaceType,
		page_size:999
	};

	$.getJSON(requestUrl, params, function(data){
		var places = data.resultData.items;
		var place;
		
		$("#div_places").html("");
		var innerHtml = "";
		clearOverlays();

		for (var i = 0; places && i < places.length; i++) {
			place = places[i];
			
			latlng = new google.maps.LatLng(place.gpsLocation.split(",")[0], place.gpsLocation.split(",")[1]);
			addMarker(latlng, place.placeName, place.id, map);
			
			innerHtml += '<div style="height: 100px; margin-left: 10px; border-bottom: 1px solid #e1e1e1; cursor: pointer;"';
			innerHtml += '	onclick="showPlaceDetail(' + place.id + ');">';
			innerHtml += '	<div style="width: 285px; float: left;">';
			innerHtml += '		<h3 style="padding: 8px 0px;">' + place.placeName + '</h3>';
			innerHtml += '		<div><img src="../image/icon_level' + place.generalSatisfaction + '_small.png"/></div>';
			innerHtml += '		<p>';
			innerHtml += '			服务满意度:';
			innerHtml += '			好' + place.goodNum + '<img src="../image/icon_red_flower.png" align="top"/>';
			innerHtml += '			中' + place.normalNum + '<img src="../image/icon_orange_flower.png" align="top"/>';
			innerHtml += '			差' + place.badNum + '<img src="../image/icon_gray_flower.png" align="top"/>';
			innerHtml += '		</p>';
			innerHtml += '		<p>评价:' + place.commentCount + '条</p>';
			innerHtml += '	</div>';
			innerHtml += '	<img src="../image/icon_merchant_small.png"';
			innerHtml += '		style="padding-top: 10px; float: left;"/>';
			innerHtml += '	<div style="clear: both;"></div>';
			innerHtml += '</div>';
		}
		$("#div_places").html(innerHtml);
	});
}

function showPlaceComments(placeId) {
	var requestUrl = contextPath + "/customer/get_place_comments";
	var params = {place_id:placeId};
	
	$.post(requestUrl, params, function(data){
		var commentHtml = "";
		var comments = data.resultData;
		
		for (var i = 0; comments && i < comments.length; i++) {
			var comment = comments[i];
			
			commentHtml += '<div style="height: 20px; padding: 7px; margin-right: 15px; background: url(\'../image/bg_comment_title.png\')">';
			commentHtml += '	<span style="float: left;">' + comment.customerName + ':</span>';
			commentHtml += '	<span style="float: right;">' + comment.commentTime + '</span>';
			commentHtml += '</div>';
			commentHtml += '<div align="left" style="padding-left: 10px;">';
			commentHtml += '	<img src="../image/icon_level' + comment.generalSatisfaction + '_small.png" align="top"/>';
			if (comment.serviceSatisfaction == "1") {
				commentHtml += '	<span style="margin-left: 20px;">好评</span><img src="../image/icon_red_flower.png" align="top" style="margin-left: 5px;"/>';
			} else if (comment.serviceSatisfaction == "2") {
				commentHtml += '	<span style="margin-left: 20px;">中评</span><img src="../image/icon_orange_flower.png" align="top" style="margin-left: 5px;"/>';
			} else {
				commentHtml += '	<span style="margin-left: 20px;">差评</span><img src="../image/icon_gray_flower.png" align="top" style="margin-left: 5px;"/>';
			}
			commentHtml += '</div>';
			commentHtml += '<div style="padding: 10px;">' + comment.comment + '</div>';
		}
		
		$("#div_place_comments").html(commentHtml);
		overlay.draw();
	}, "json");
}
 
function toggleTypeList() {
	if ($("#slt_type_list").css("display") == "none") {
		$("#slt_type_list").css("left", $("#slt_type").offset().left + 2);
		$("#slt_type_list").css("top", $("#slt_type").offset().top + 42);
		$("#slt_type_list").css("display", "");
	} else {
		$("#slt_type_list").css("display", "none");
	}
	
	$("#slt_district_list").css("display", "none");
	$("#slt_place_type_list").css("display", "none");
}

function toggleDistrictList() {
	if ($("#slt_district_list").css("display") == "none") {
		$("#slt_district_list").css("left", $("#slt_district").offset().left + 2);
		$("#slt_district_list").css("top", $("#slt_district").offset().top + 42);
		$("#slt_district_list").css("display", "");
	} else {
		$("#slt_district_list").css("display", "none");
	}
	
	$("#slt_type_list").css("display", "none");
	$("#slt_place_type_list").css("display", "none");
}

function togglePlaceTypeList() {
	if ($("#slt_place_type_list").css("display") == "none") {
		$("#slt_place_type_list").css("left", $("#slt_place_type").offset().left + 2);
		$("#slt_place_type_list").css("top", $("#slt_place_type").offset().top + 42);
		$("#slt_place_type_list").css("display", "");
	} else {
		$("#slt_place_type_list").css("display", "none");
	}
	
	$("#slt_type_list").css("display", "none");
	$("#slt_district_list").css("display", "none");
}